package com.hzz.web.boss.examine;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hzz.api.examine.IExamineEvaluateService;
import com.hzz.api.examine.IExamineTargetDetailService;
import com.hzz.dto.examine.ExamineEvaluateDto;
import com.hzz.dto.examine.ExamineTargetDetailDto;
import com.hzz.dto.user.UserDto;
import com.hzz.framework.PageUtil;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.ALogOperation;
import com.hzz.framework.annotation.RfUser2Bean;
import com.hzz.framework.annotation.auth.Logical;
import com.hzz.framework.annotation.auth.RequiresPermissions;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.JwtUtil;
import com.hzz.framework.web.controller.BaseController;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

@Api(description = "目标考核-考核评估")
@RestController
@Slf4j
public class ExamineEvaluateController extends BaseController {
	@Autowired
	IExamineEvaluateService examineEvaluateService;
	static final String acPrefix = "/boss/examine/examineEvaluate/";

	@Autowired
	IExamineTargetDetailService examineTargetDetailService;
	/**
	 * 
	 * @param dto
	 * @param pageNum
	 * @param flag
	 *            用来标记是不是考核自评查询请求，true是
	 * @return
	 */
	@RequiresPermissions("examineEvaluate:menu")
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "page/{pageNum}")
	// @ApiOperation(value = "信息分页(未删除)")
	public Response page(@ModelAttribute ExamineEvaluateDto dto, @PathVariable("pageNum") Integer pageNum,
			boolean flag) {
		log.info("page.........");
		Response result = new Response();
		try {
			dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
			dto.setPageNum(pageNum);
			dto.setDelFlag(0);
			if (flag) {
				UserDto userDto = JwtUtil.getSubject(UserDto.class);
				dto.setSubordinateIds(userDto.getId() + "");
				dto.setDeadline(new Date());
			}
			result.data = PageUtil.copy(examineEvaluateService.findDataIsPage(dto));
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@RequiresPermissions("examineEvaluate:info")
	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "info/{id}")
	// @ApiOperation(value = "信息详情")
	public Response info(@PathVariable("id") Long id) {
		log.info("info.........");
		Response result = new Response();
		try {
			ExamineEvaluateDto dto = new ExamineEvaluateDto();
			dto.setId(id);
			dto.setDelFlag(0);
			result.data = examineEvaluateService.findDataById(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@RequiresPermissions("examineEvaluate:del")
	@RequestMapping(method = RequestMethod.POST, value = acPrefix + "del/{id}")
	@ALogOperation(type = "删除", desc = "目标考核-考核评估-逻辑删除")
	// @ApiOperation(value = "逻辑删除")
	public Response del(@PathVariable("id") Long id) {
		log.info("del.........");
		Response result = new Response(0, "success");
		try {
			ExamineEvaluateDto dto = new ExamineEvaluateDto();
			dto.setId(id);
			result.message = examineEvaluateService.deleteDataById(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	@RequiresPermissions(value = { "examineEvaluate:add", "examineEvaluate:edit" }, logical = Logical.OR)
	@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT }, value = acPrefix + "save")
	@RfUser2Bean
	@ALogOperation(type = "修改", desc = "目标考核-考核评估")
	// @ApiOperation(value = "信息保存")
	public Response save(@Validated @ModelAttribute ExamineEvaluateDto dto, BindingResult bindingResult) {
		log.info("save.........");
		Response result = new Response(0, "success");
		try {
			// if ("1".equals(request.getSession().getAttribute(acPrefix +
			// "save." + dto.getToken()))) {
			// throw new RuntimeException("请不要重复提交!");
			// }
			if (bindingResult.hasErrors()) {
				String errorMsg = "";
				List<ObjectError> errorList = bindingResult.getAllErrors();
				for (ObjectError error : errorList) {
					errorMsg += (error.getDefaultMessage()) + ";";
				}
				result = Response.error(errorMsg);
			} else {
				// 查询考核目标明细
				ExamineTargetDetailDto targetDetailDto = new ExamineTargetDetailDto();
				targetDetailDto.setTargetId(dto.getTargetId());
				List<ExamineTargetDetailDto> targetDetails = this.examineTargetDetailService
						.findDataIsList(targetDetailDto);
				if (targetDetails == null || targetDetails.isEmpty()) {
					result = Response.error("请完善考核目标明细后再操作！");
					return result;
				}

				result = examineEvaluateService.saveOrUpdateData(dto);
				// request.getSession().setAttribute(acPrefix + "save." +
				// dto.getToken(), "1");
			}
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}
}